home *** CD-ROM | disk | FTP | other *** search
/ Amoszine 5 / Amoszine 5.adf / arts / Chapter29.asc.cm / Chapter29.asc.cm
Text File  |  1992-02-26  |  5KB  |  223 lines

  1. <>{The Absolute Beginners Guide To Amos@6
  2.  
  3. }Chapter Twenty Nine
  4. @4
  5. Imagine an enormous game like Populous
  6. or Elite without a save game option!
  7.  
  8. They would be almost unplayable
  9. without it.
  10.  
  11. I would now like to show you how to
  12. set up a load and save game feature
  13. for your own programs.
  14.  
  15. It's a fairly straight forward affair
  16. doing this in Amos but it has NEVER
  17. been covered anywhere, not in the Amos
  18. manual, books or magazines that I have
  19. seen.  So how is a beginner meant to
  20. know how to do this?
  21.  
  22. @6
  23. Don't worry, read on....
  24.  
  25.  
  26.  
  27.  
  28. @4
  29. When you save a games position all you
  30. have to do is save ALL of the
  31. variables into a file.  It's as simple
  32. as that!
  33.  
  34. First, decide where you are going to
  35. put your saved game feature, the main
  36. menu would be the usual place. Set up
  37. your menu with LOAD and SAVE in it 
  38. and when selected by the user call the
  39. Procedures _Load and _Save 
  40. respectively.
  41.  
  42.  
  43. We of course, need to write the _Load
  44. and _Save procedures first.
  45.  
  46.  
  47. Let's look at writing _Save first.
  48.  
  49. First of all we need to have a list of
  50. EVERY variable used in the game, you
  51. may not actually need to save all of
  52. them, but if you are not sure about
  53. any particular one then include it
  54. anyway. 
  55.  
  56.  
  57. Once you have written down all of your
  58. programs variables you are ready to
  59. write the _Save game procedure.
  60.  
  61.  
  62. To keep this example simple we will
  63. not offer the user a file selector but
  64. we will save the games position with a
  65. predefined name. This means only one
  66. game can ever be saved on a single
  67. disk as any subsequent saves will 
  68. overwrite the last save. This keeps
  69. things very simple and will hopefully
  70. mean you will understand it a lot
  71. easier.
  72.  
  73. Of course, once you have learnt the
  74. basics of loading and saving variables
  75. you can expand these procedures into
  76. your own custom routines, specific to
  77. your program.
  78.  
  79. I am going to make up an imaginary
  80. game that has only four variables in
  81. it.   The variables are:
  82. @1
  83. POINTS
  84. FRUITS (5)
  85. MONSTER$(25)
  86. FIRE$
  87. @4
  88. As we now know all of the variables of
  89. our game we can PRINT them to a disk!
  90.  
  91. But first we must OPEN a channel.  You
  92. do not need to understand how this
  93. works,just remember that when you OPEN
  94. a channel it MUST be closed after you
  95. have finished with it.
  96. @1
  97. Open Out 1,"DF0:SAVED_GAME"
  98. @4
  99. This OPENs a channel which we will
  100. call 1 for our reference. You can have
  101. more than one channel open at a time,
  102. but I doubt you will ever find the
  103. need to, so just use 1 all of the
  104. time. The "DF0:SAVED_GAME" is the path
  105. and file name we will be creating on
  106. the disk. Remember the comma and the
  107. quotes around the file name.  You can
  108. put in any path or call the file
  109. anything you wish of course.
  110.  
  111. Now we can get to work on actually
  112. saving the variables onto the disk.
  113.  
  114. @1
  115. PRINT #1,POINTS
  116. @4
  117.  
  118. This line will save the variable
  119. POINTS and it's current value to the
  120. file we have called SAVED_GAME.
  121. The #1 tells Amos we are printing to
  122. channel one.
  123.  
  124. Now we want to save the dimensioned
  125. array FRUITS() which has five elements
  126. - this too is fairly simple.
  127. @1
  128. For A=1 To 5
  129. Print #1,FRUITS(A)
  130. Next A
  131. @4
  132. We have used a For Next loop to put
  133. the five elements onto disk which is
  134. stored along with POINTS in the file
  135. "SAVED_GAME"
  136.  
  137. Next we have MONSTER$() a 25 element
  138. string array. This is virtually the
  139. same.
  140. @1
  141. For A=1 To 25
  142. Print #1,MONSTER$(A)
  143. Next A
  144. @4
  145. And lastly, we have a nice simple
  146. string variable.
  147. @1
  148. PRINT #1,FIRE$
  149. @4
  150. Don't forget to CLOSE channel 1,
  151. @1
  152. CLOSE 1
  153. @4
  154. And that's it, all the four variables
  155. and their values are all stored neatly
  156. into one file on disk ready to restore
  157. the game to it's original position. 
  158.  
  159. So, lumping it all together, here is
  160. the Procedure in full.
  161. @1
  162. Procedure _SAVE
  163.  
  164. Open Out 1,"DF0:SAVED_GAME"
  165.  
  166. PRINT #1,POINTS
  167.  
  168. For A=1 To 5
  169. Print #1,FRUITS(A)
  170. Next A
  171.  
  172. For A=1 To 25
  173. Print #1,MONSTER$(A)
  174. Next A
  175.  
  176. PRINT #1,FIRE$
  177.  
  178. CLOSE 1
  179. End Proc
  180. @4
  181. Obviously to make use of this
  182. procedure in your own programs you
  183. will need to enter the variables
  184. pertaining to your own particular
  185. program.
  186.  
  187. Now we must look at how to load back
  188. the variables into your program. 
  189.  
  190. Luckily it's all very simple if you
  191. understood how the save part works.
  192.  
  193. All we need to do is substitute the
  194. PRINT statements for INPUT statements.
  195. So all we have to do is cut and paste
  196. the _save procedure, rename it _LOAD
  197. and change the PRINTs to INPUTs.
  198. @1
  199. Procedure _LOAD
  200. Open In 1,"df0:SAVED_GAME"
  201.  
  202. Input #1,POINTS
  203.  
  204. For A=1 To 5
  205. Input #1,FRUITS(A)
  206. Next A
  207.  
  208. For A=1 To 25
  209. Input #1,MONSTER$(A)
  210. Next A
  211.  
  212. Input #1,FIRE$
  213.  
  214. CLOSE 1
  215. End Proc
  216. @4
  217. See example29.Amos for a small
  218. demonstration.
  219.  
  220. @6
  221. End of Chapter Twenty Nine
  222. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  223.